import { ipcRenderer, contextBridge } from 'electron'

contextBridge.exposeInMainWorld('api', {
  on(...args: Parameters<typeof ipcRenderer.on>) {
    const [channel, listener] = args
    return ipcRenderer.on(channel, (event, ...args) => listener(event, ...args))
  },
  off(...args: Parameters<typeof ipcRenderer.off>) {
    const [channel, ...omit] = args
    return ipcRenderer.off(channel, ...omit)
  },
  send(...args: Parameters<typeof ipcRenderer.send>) {
    const [channel, ...omit] = args
    return ipcRenderer.send(channel, ...omit)
  },
  invoke(...args: Parameters<typeof ipcRenderer.invoke>) {
    const [channel, ...omit] = args
    return ipcRenderer.invoke(channel, ...omit)
  },
  getInstalledApps: () => ipcRenderer.invoke('get-installed-apps'),
  openInstalledApp: (appPath: any) => ipcRenderer.invoke('open-installed-app', appPath),
  winClose: () => ipcRenderer.invoke('win-close'),
  getAppIcon: (appPath: string) => ipcRenderer.invoke('getAppIcon', appPath)
})
